//
//  QuestionCommentListController.m
//  suRead
//
//  Created by nancy  on 16/9/18.
//  Copyright © 2016年 bao. All rights reserved.
//
#import "QuestionCommentListController.h"
#import "QuestionCommentCell.h"
#import "Masonry.h"
#import "UIControl+Blocks.h"
#import "ServiceNews.h"
#import "M_QuestionComment.h"
#import "SendCommentController.h"
#import "UIViewController+HUD.h"

@interface QuestionCommentListController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bottomBarView;
@property (nonatomic,weak) IBOutlet UITableView *newsCommentTableView;
@property (nonatomic,strong)NSMutableArray *commentArray;
@end

@implementation QuestionCommentListController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self setNeedsStatusBarAppearanceUpdate];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    __weak typeof(self) weak_self = self;
    _bottomBarView.backgroundColor = [UIColor colorWithRed:251/255.0f green:251/255.0f blue:251/255.0f alpha:1.0f];
    
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonBack setImage:[UIImage imageNamed:@"button_back"] forState:UIControlStateNormal];
    [buttonBack addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        //返回
        [weak_self.navigationController popViewControllerAnimated:YES];
        [weak_self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }];
    
    UIButton *buttonWriteComment = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonWriteComment.layer.cornerRadius = 5.0f;
    buttonWriteComment.layer.masksToBounds = YES;
    buttonWriteComment.layer.borderWidth = .5f;
    buttonWriteComment.layer.borderColor = [UIColor lightGrayColor].CGColor;
    buttonWriteComment.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [buttonWriteComment setTitleColor:[UIColor lightGrayColor]forState:UIControlStateNormal];
    buttonWriteComment.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    buttonWriteComment.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [buttonWriteComment setTitle:@"回复" forState:UIControlStateNormal];
    [buttonWriteComment addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        //写评论
        SendCommentController *controller = [SendCommentController new];
        controller.f_wbreplyid= self.question_id;
        controller.type=@"one";
        [weak_self.navigationController pushViewController:controller animated:YES];
    }];
    
    UILabel *linelab = [UILabel new];
    linelab.backgroundColor = [UIColor colorWithRed:160/255.0f green:160/255.0f blue:160/255.0f alpha:1.0f];
    [_bottomBarView addSubview:linelab];
    
    
    [_bottomBarView addSubview:buttonBack];
    [_bottomBarView addSubview:buttonWriteComment];

    
    [linelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(_bottomBarView);
        make.height.equalTo(@0.5f);
    }];
    
    [buttonBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_bottomBarView);
        make.left.equalTo(_bottomBarView).with.offset(8.0f);
        make.width.equalTo(@28.0f);
        make.height.equalTo(@28.0f);
    }];
    
    [buttonWriteComment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(buttonBack.mas_right).offset(8.0f);
        make.right.equalTo(_bottomBarView).with.offset(-10.0f);
        make.top.equalTo(_bottomBarView).offset(8.0f);
        make.bottom.equalTo(_bottomBarView).offset(-8.0f);
        
    }];
    
    self.newsCommentTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.newsCommentTableView.sectionFooterHeight = 0;
    [self.newsCommentTableView registerNib:[UINib nibWithNibName:@"QuestionCommentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"QuestionCommentCell"];
    [self showHUDProgress:@""];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    __weak typeof(self) weak_self = self;
    [ServiceNews doFetchQuestionComments:self.commentsUrl
                            success:^(id responseObject, NSString *message) {
                                self.commentArray = [NSMutableArray new];
                                [self.commentArray addObjectsFromArray:responseObject];
                                [self.newsCommentTableView reloadData];
                                [weak_self removeHUDProgress];
                            }
                            failure:^(NSString *message) {
                                [weak_self removeHUDProgress];
                            }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.commentArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_QuestionComment *commentData = [self.commentArray objectAtIndex:indexPath.section];
    return [QuestionCommentCell heightWithContent:commentData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QuestionCommentCell *cell = (QuestionCommentCell *)[tableView dequeueReusableCellWithIdentifier:@"QuestionCommentCell"];
    M_QuestionComment *commentData = [self.commentArray objectAtIndex:indexPath.section];
    [cell setCellData:commentData];
    
    __weak typeof(self) weak_self = self;
    [cell setReplyClickCallBack:^(){//回复
        SendCommentController *controller = [SendCommentController new];
        controller.f_wbreplyid= commentData.f_wbreplyid;
        controller.commentid = commentData.f_id;
        controller.type=@"two";
        [weak_self.navigationController pushViewController:controller animated:YES];
    }];
    [cell setZanClickCallBack:^(){//赞
        [ServiceNews doZanQuestion:commentData.f_id
                              type:@"comment"
                           success:^(id responseObject, NSString *message) {
                           } failure:^(NSString *message) {
                           }];
    }];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
